package com.yxmadoc.umlproject.controller;

import com.alibaba.fastjson.JSONObject;
import com.yxmadoc.umlproject.config.enums.ReturnResult;
import com.yxmadoc.umlproject.model.Order;
import com.yxmadoc.umlproject.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/order")
    public JSONObject insert(@RequestBody Order order){
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), orderService.save(order));
        return data;
    }

    @DeleteMapping("/order/{orderId}")
    public JSONObject delete(@PathVariable("orderId")Integer orderId){
        JSONObject result = new JSONObject();
        orderService.delete(orderId);
        result.put(ReturnResult.STATUS.getCode(), true);
        return result;
    }

    @GetMapping("/order/{orderId}")
    public JSONObject detail(@PathVariable("orderId")Integer orderId){
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), orderService.findOne(orderId));
        return data;
    }

    @GetMapping("/order/list/customer/{customerId}")
    public JSONObject listBYCustomer(@PathVariable("customerId")String customerId, @PageableDefault(sort = {"orderId"}, direction = Sort.Direction.DESC)Pageable pageable){
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), orderService.listByCustomer(customerId, pageable));
        return data;
    }

    @GetMapping("/order/list/repairer/{repairerId}")
    public JSONObject listByRepairer(@PathVariable("repairerId")String repairerId, @PageableDefault(sort = {"orderId"}, direction = Sort.Direction.DESC)Pageable pageable){
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), orderService.listByRepairer(repairerId, pageable));
        return data;
    }

    @GetMapping("/order/{orderId}/selection/{priceId}")
    public JSONObject selection(@PathVariable("orderId")Integer orderId, @PathVariable("priceId")Integer priceId){
        JSONObject result = new JSONObject();
        orderService.customerRepairerSelection(priceId, orderId);
        result.put(ReturnResult.STATUS.getCode(), true);
        return result;
    }

    @GetMapping("/order/complete/{orderId}")
    public JSONObject complete(@PathVariable("orderId") Integer orderId) {
        JSONObject result = new JSONObject();
        result.put(ReturnResult.DATA.getCode(), orderService.completeOrder(orderId));
        return result;
    }

    @GetMapping("/order/listByQuotatedFalse")
    public JSONObject listByQuotatedFalse(@PageableDefault(sort = {"orderId"}, direction = Sort.Direction.DESC) Pageable pageable) {
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), orderService.listByQuotatedFalse(pageable));
        return data;
    }

    @GetMapping("/order/listByCommentedFalse/{customerId}")
    public JSONObject listByCommentedFalse(@PathVariable("customerId") String customerId, @PageableDefault(sort = {"orderId"}, direction = Sort.Direction.DESC) Pageable pageable) {
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), orderService.listByCommentedFalse(customerId, pageable));
        return data;
    }

    @PostMapping("/order/comment")
    public JSONObject comment(@RequestBody Order order) {
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), orderService.comment(order));
        return data;
    }

}
